/**
 * 
 */
package com.zouqone.smc.modules.sys.fiter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zouqone.smc.modules.sys.entity.User;

/**
 * 登录认证过滤器
 * @author zouqone
 * @date 2016年1月2日  下午6:01:06
 */
public class LoginFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String path = req.getRequestURL().toString();
		String front = req.getContextPath();
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null && path.indexOf("/login")==-1){
			resp.sendRedirect(front+"/a/login");
		}else{
			chain.doFilter(request, response);
		}
		
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	
}
